<template>
  <div class="centre">
    <div class="cenboxs">
      <div class="header_cen">
        <div class="header_img">
          <img :src="PointList.avatar" alt="" style="cursor: pointer" @click="Edits(2)" />
        </div>
        <div class="card_name">
          <div class="aa">{{ PointList.nickname }}</div>
          <div class="bb">
            <div class="cc">{{ PointList.age == '' ? '' : PointList.age + '岁' }}</div>
            <div class="myxg" style="display: flex; cursor: pointer" @click="Edits(5)">
              <div>
                <el-icon>
                  <Edit />
                </el-icon>
              </div>
              &nbsp;
              <div style="color: #e02020" class="xxx">修改</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="fen">
      <div class="jf">
        <p>{{ zong }}</p>
        <h5>累计总积分</h5>
      </div>
      <div class="fist"></div>

      <div class="jf">
        <p>{{ PointList.exam }}</p>
        <h5>答题积分</h5>
      </div>
      <div class="jf">
        <p>{{ PointList.matrix }}</p>
        <h5>学习矩阵积分</h5>
      </div>
      <div class="jf">
        <p>{{ PointList.network }}</p>
        <h5>最美网文积分</h5>
      </div>
      <div class="jf">
        <p>{{ PointList.video }}</p>
        <h5>看视频积分</h5>
      </div>
      <div class="jf">
        <p>{{ PointList.todo }}</p>
        <h5>完成待办积分</h5>
      </div>
    </div>
    <div class="con">
      <div class="c_item">
        <div class="i_lef">所在党支部：</div>
        <div class="i_rig">
          <div class="i_con">{{ data.dept }}</div>
          <div class="rig"></div>
        </div>
      </div>
      <div class="br"></div>
      <div class="c_item">
        <div class="i_lef">入党年月：</div>
        <div class="i_rig">
          <!-- <div class="i_con">{{ data.join }}</div> -->
          <div class="i_con">{{ list[0] }}年{{ list[1] }}月</div>
          <div class="rig" style="cursor: pointer" @click="Edits(1)">
            <div style="display: flex; cursor: pointer" @click="Edits(4)">
              <div>
                <el-icon>
                  <Edit />
                </el-icon>
              </div>
              &nbsp;
              <div style="color: #e02020" class="xxx">修改</div>
            </div>
          </div>
        </div>
      </div>
      <div class="br"></div>
      <div class="c_item">
        <div class="i_lef">党龄：</div>
        <div class="i_rig">
          <div class="i_con">{{ data.join_age }}</div>
          <div class="rig"></div>
        </div>
      </div>
      <div class="br"></div>
      <div class="c_item">
        <div class="i_lef">党内职务：</div>
        <div class="i_rig">
          <div class="i_con">{{ data.position_text }}</div>
          <div class="rig"></div>
        </div>
      </div>
      <div class="br"></div>
      <div class="c_item">
        <div class="i_lef">个人荣誉：</div>
        <div class="i_rig">
          <div class="i_con">
            <div class="cc" v-for="(item, i) in data.honor" :key="i">
              <div class="c_a">{{ item.date }}</div>
              <div class="c_b">{{ item.content }}</div>
            </div>
          </div>
          <div class="rig" style="cursor: pointer" @click="Edits(3)">
            <div style="display: flex">
              <div>
                <el-icon>
                  <Edit />
                </el-icon>
              </div>
              &nbsp;
              <div style="color: #e02020" class="xxx">修改</div>
            </div>
          </div>
        </div>
      </div>
      <div class="br"></div>
    </div>
    <div class="foot"></div>
    <div class="zhe" v-show="flag">
      <div class="zhao">
        <div style="display: flex; justify-content: space-between; padding: 15px">
          <h3>
            {{ nei == 1 ? '入党年月' : nei == 2 ? '修改头像' : '个人荣誉' }}
          </h3>
          <!-- <div @click="cha" style="cursor: pointer">X</div> -->
          <img class="e_rig" style="cursor: pointer" @click="cha" src="@/image/xxx.png" alt="" />
        </div>
        <div style="margin: 15px 0">
          <div class="rudang" v-if="nei == 1">
            <div class="rudang1">入党时间</div>
            <el-date-picker v-model="value1" type="date" class="prickertime" />
          </div>
        </div>
        <div class="mybtn">
          <el-button type="primary" class="quxiao" @click="qu">取消</el-button>
          <el-button type="primary" class="guanbi" @click="bao">保存</el-button>
        </div>
      </div>
    </div>
    <div class="zhe" v-show="flag5">
      <div class="zhao">
        <div style="display: flex; justify-content: space-between; padding: 15px">
          <h3>出生年月</h3>
          <!-- <div @click="cha" style="cursor: pointer">X</div> -->
          <img class="e_rig" style="cursor: pointer" @click="cha" src="@/image/xxx.png" alt="" />
        </div>
        <div style="margin: 15px 0">
          <div class="rudang">
            <div class="rudang1">出生年月</div>
            <el-date-picker v-model="value2" type="date" class="prickertime" />
          </div>
        </div>
        <div class="mybtn">
          <el-button type="primary" class="quxiao" @click="qu">取消</el-button>
          <el-button type="primary" class="guanbi" @click="bao5">保存</el-button>
        </div>
      </div>
    </div>
    <div class="zhe" v-show="flag1">
      <div class="zhao">
        <div style="display: flex; justify-content: space-between; padding: 15px">
          <h3>
            {{ nei == 1 ? '入党年月' : nei == 2 ? '修改头像' : '个人荣誉' }}
          </h3>
          <!-- <div @click="cha" style="cursor: pointer">X</div> -->
          <img class="e_rig" style="cursor: pointer" @click="cha" src="@/image/xxx.png" alt="" />
        </div>
        <div class="mytx">
          <div>图片规格：不可大于1M</div>
          <div>图片尺寸：建议为160px*160px</div>
          <div>图片格式：必须使用png、bmp、jpg、jpeg</div>
        </div>
        <div style="margin: 15px 0">
          <el-upload class="avatar-uploader" :auto-upload="false" accept="image/*" :show-file-list="false"
            :before-upload="beforeAvatarUpload" :on-change="avatarFileChange">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">上传头像</el-icon>
            <!-- 上传头像 -->
          </el-upload>
        </div>
        <div class="mybtn">
          <el-button type="primary" class="quxiao" @click="qu">取消</el-button>
          <el-button type="primary" class="guanbi" @click="bao1">保存</el-button>
        </div>
      </div>
    </div>
    <div class="zhe" v-show="flag2">
      <div class="zhao zhao1">
        <div style="display: flex; justify-content: space-between; padding: 15px">
          <h3>
            {{ nei == 1 ? '入党年月' : nei == 2 ? '修改头像' : '个人荣誉' }}
          </h3>
          <!-- <div @click="cha" style="cursor: pointer">X</div> -->
          <img class="e_rig" style="cursor: pointer" @click="cha" src="@/image/xxx.png" alt="" />
        </div>
        <div style="margin: 15px 0">
          <div v-for="(item, i) in myjson">
            <div class="rudang">
              <div class="rudang1">获取时间</div>
              <el-date-picker v-model="item.date" type="date" class="prickertime" />
              <div style="
                  display: flex;
                  justify-content: space-around;
                  margin-left: 20px;
                  cursor: pointer;
                " @click="mydel(i)">
                <div>
                  <el-icon>
                    <Delete />
                  </el-icon>
                </div>
                &nbsp;
                <div style="color: #e02020">删除</div>
              </div>
            </div>
            <div class="rudang">
              <div class="rudang1">荣誉名称</div>
              <div class="run2">
                <el-input v-model="item.content" placeholder="请输入" />
              </div>
            </div>
          </div>
          <el-button type="danger" class="tj" @click="tj">添加</el-button>
        </div>
        <div class="mybtn">
          <el-button type="primary" class="quxiao" @click="qu">取消</el-button>
          <el-button type="primary" class="guanbi" @click="bao2">保存</el-button>
        </div>
      </div>
    </div>
    <div class="zhe" v-show="flag3">
      <div class="zhao">
        <div style="display: flex; justify-content: space-between; padding: 15px">
          <h3>添加荣誉</h3>
          <!-- <div @click="cha" style="cursor: pointer">X</div> -->
          <img class="e_rig" style="cursor: pointer" @click="cha" src="@/image/xxx.png" alt="" />
        </div>
        <div style="margin: 15px 0">
          <div>
            <div class="rudang">
              <div class="rudang1">获取时间</div>
              <el-date-picker v-model="mytj[0].date" type="date" class="prickertime" />
            </div>
            <div class="rudang">
              <div class="rudang1">荣誉名称</div>
              <div class="run2">
                <el-input v-model="mytj[0].content" placeholder="请输入" />
              </div>
            </div>
          </div>
        </div>
        <div class="mybtn">
          <el-button type="primary" class="quxiao" @click="qu">取消</el-button>
          <el-button type="primary" class="guanbi" @click="bao3">保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { deptInfo, deptPoint, germembereditApi, reqUpload } from '@/api/user'
import { ref } from 'vue'
import moment from 'moment'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'
// 弹框
let flag: any = ref(false)
let flag1: any = ref(false)
let flag2: any = ref(false)
let flag3: any = ref(false)
let flag5: any = ref(false)

let nei = ref()
const value1 = ref()
//修改基本信息弹框
const Edits = (e: any) => {
  console.log(e)
  nei.value = e
  if (e == 1) {
    flag.value = true
  } else if (e == 2) {
    flag1.value = true
  } else if (e == 3) {
    flag2.value = true
  } else if (e == 5) {
    flag5.value = true
  }
}
const myjson: any = ref([
  {
    date: '',
    content: ''
  }
])
const mytj: any = ref([
  {
    date: '',
    content: ''
  }
])
const tj = () => {
  flag2.value = false
  flag3.value = true
}
const mydel = (e: any) => {
  myjson.value.splice(e, 1)
  myjson.value = myjson.value.map((item: any) => {
    item.date = moment(item.date).format('YYYY-MM-DD')
    return item
    // return (item.date = moment(item.date).format('YYYY-MM-DD'))
  })
  arr.value.field = 'honor'
  arr.value.value = JSON.stringify(myjson.value)
  germembereditApi(arr.value).then(res => {
    ElMessage.success('已删除!')
    dept()
  })
}
const bao3 = () => {
  mytj.value[0].date = moment(mytj.value[0].date).format('YYYY-MM-DD')
  arr.value.field = 'honor'
  if (mytj.value[0].date == 'Invalid date') {
    ElMessage.error('不能为空！')
  } else {
    myjson.value = [...myjson.value, ...mytj.value]
    arr.value.value = JSON.stringify(myjson.value)
    germembereditApi(arr.value).then(res => {
      console.log(res, '保存')
      flag3.value = false
      dept()
      mytj.value = [{ date: '', content: '' }]
    })
  }

  // console.log(mytj.value)
}
const bao2 = () => {
  myjson.value = myjson.value.map((item: any) => {
    item.date = moment(item.date).format('YYYY-MM-DD')
    return item
    // return (item.date = moment(item.date).format('YYYY-MM-DD'))
  })
  if (myjson.value[0].date == 'Invalid date' || myjson.value[0].content == '') {
    ElMessage.error('不能为空！')
  } else {
    arr.value.field = 'honor'
    arr.value.value = JSON.stringify(myjson.value)
    germembereditApi(arr.value).then(res => {
      console.log(res, '保存')
      flag2.value = false
      dept()
    })
  }
}

const cha = () => {
  flag.value = false
  flag1.value = false
  flag2.value = false
  flag3.value = false
  flag5.value = false
}
const qu = () => {
  flag.value = false
  flag1.value = false
  flag2.value = false
  flag3.value = false
  flag5.value = false
}
const arr = ref({
  field: '',
  value: ''
})
const bao = () => {
  arr.value.field = 'join'
  arr.value.value = moment(value1.value).format('YYYY-MM-DD')
  console.log(arr.value, '保存')
  if (arr.value.value == 'Invalid date') {
    ElMessage.error('不能为空！')
  } else {
    germembereditApi(arr.value).then(res => {
      flag.value = false
      dept()
    })
  }
}
const avatarFile = ref({})
const imageUrl = ref('')
function dataURLtoFile(dataURL: any) {
  let arr = dataURL.split(',')
  let mime = arr[0].match(/:(.*?);/)[1]
  let bstr = atob(arr[1])
  let n = bstr.length
  let u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new File([u8arr], `${Date.now}.jpg`, {
    type: mime
  })
}
// const handleAvatarSuccess: UploadProps['onSuccess'] = (
//   response,
//   uploadFile
// ) => {
//   imageUrl.value = URL.createObjectURL(uploadFile.raw!)
// }
const avatarFileChange = (e: { raw?: any }) => {
  avatarFile.value = e
  imageUrl.value = URL.createObjectURL(e.raw)
}
const beforeAvatarUpload: UploadProps['beforeUpload'] = async (
  rawFile: any
) => {
  if (
    rawFile.type !== 'image/jpeg' &&
    rawFile.type !== 'image/png' &&
    rawFile.type !== 'image/jpg' &&
    rawFile.type !== 'image/bmp'
  ) {
    ElMessage.error('格式不正确!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 1) {
    ElMessage.error('图片大小不能超过1MB!')
    return false
  }
  // let res = await reqUpload(rawFile)
  // imageUrl.value = res.data.data.uri
  // console.log(res)

  return false
}
const bao1 = () => {
  // 将压缩后的图片上传到服务器
  let img = new Image()
  img.src = imageUrl.value

  // 等待图片加载完成
  img.onload = async () => {
    // 创建canvas元素
    let canvas = document.createElement('canvas')
    let ctx: any = canvas.getContext('2d')

    // 设置canvas尺寸
    let width = img.width
    let height = img.height
    let scale = width / height
    let maxWidth = 800
    let maxHeight = 800 / scale
    canvas.width = maxWidth
    canvas.height = maxHeight

    // 绘制图片
    ctx.drawImage(img, 0, 0, maxWidth, maxHeight)

    // 将canvas转换为base64格式的图片
    let base64 = canvas.toDataURL('image/jpeg', 0.8)
    // 将base64格式的图片转换为File对象
    let file = dataURLtoFile(base64)
    avatarFile.value.raw = file
    let { data } = await reqUpload(avatarFile.value.raw)
    if (data.code != 1) {
      return ElMessage.error('上传失败！')
    }
    // flag1.value = false
    arr.value.field = 'avatar'
    arr.value.value = data.data.url
    console.log(arr.value, 'arr')

    germembereditApi(arr.value).then(res => {
      // console.log(res, 'aa')
      // dept()
      flag1.value = false
      deptPoint1()
    })
  }
}
const value2 = ref()
const bao5 = () => {
  // flag1.value = false
  arr.value.field = 'birthday'
  arr.value.value = moment(value2.value).format('YYYY-MM-DD')
  console.log(arr.value, 'arr')
  germembereditApi(arr.value).then(res => {
    // console.log(res, 'aa')
    // dept()
    flag5.value = false
    deptPoint1()
  })
}
// const mybao5 = () => {
//   arr.value.field = 'birthday'
//   arr.value.value = '2022-02-08'
//   germembereditApi(arr.value).then(res => {
//     // console.log(res, 'aa')
//     // dept()
//     flag5.value = false
//     deptPoint1()
//   })
// }
// mybao5()
// 上面
const data = ref({
  dept: '',
  dept_id: '',
  honor: [{ date: '', content: '' }],
  join: '',
  id: '',
  join_age: '',
  position: '',
  position_text: '',
  self: ''
})
let zong: any = ref<any>(0)
let PointList = ref({
  age: '',
  avatar: '',
  exam: '',
  id: '',
  matrix: '',
  network: '',
  nickname: '',
  point: '',
  todo: '',
  video: ''
})
const list = ref([])
const dept = () => {
  deptInfo().then(res => {
    data.value = res.data.data
    value1.value = res.data.data.join
    list.value = res.data.data.join.split('-')
    console.log(list.value, '详情')
    if (data.value.honor.length > 0) {
      myjson.value = data.value.honor
      console.log(myjson.value)
    }
  })
}
dept()
const deptPoint1 = () => {
  deptPoint().then(res => {
    console.log(res, 'xg')

    PointList.value = res.data.data
    if (zong.value == 0) {
      zong.value +=
        PointList.value.exam +
        PointList.value.matrix +
        PointList.value.network +
        PointList.value.todo +
        PointList.value.video
    }
  })
}
deptPoint1()
</script>

<style lang="scss" scoped>
.e_rig {
  width: 16px;
  height: 16px;
}

.mybtn {
  margin-top: 50px;
  text-align: right;
}

.quxiao {
  width: 96px;
  height: 40px;
  color: #999999;
  border: 1px solid #999999;
  font-size: 16px;
  font-weight: 500;
  background: #f5f5f5;
  cursor: pointer;
}

.guanbi {
  width: 96px;
  height: 40px;
  color: #fff;
  font-size: 16px;
  font-weight: 500;
  background: #e02020;
  cursor: pointer;
}

.el-icon svg {
  color: #e02020;
  // font-size: 50px;
}

.tj {
  margin-left: 50px;
  margin-top: 30px;
  width: 96px;
  height: 40px;
  border: none;
  background-color: pink;
  cursor: pointer;
  color: #e02020;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
  margin-left: 25px;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

.zhe {
  position: fixed;
  width: 100%;
  height: 100%;
  background: #000000a0;
  top: 0;
  left: 0;
  z-index: 9;

  .zhao {
    width: 608px;
    background: #fff;
    padding: 15px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -304px;
    margin-top: -250px;
    position: relative;

    .rudang {
      display: flex;
      font-family: 30px;
      align-items: center;
      margin-top: 30px;

      .el-icon {
        margin-top: 3px;
      }

      .rudang1 {
        width: 100px;
        margin-left: 50px;
        margin-right: 20px;
      }

      .run2 {
        width: 300px;
      }
    }
  }

  .zhao1 {
    height: 600px;
    overflow: auto;
  }

  .zhao1::-webkit-scrollbar {
    display: none;
  }
}

.centre {
  margin-top: -18px;
  margin-left: -16px;
  width: 800px;
  background-color: #fff;

  .foot {
    height: 500px;
  }

  .con {
    padding: 0 16px;
    margin-top: 37px;

    .c_item {
      display: flex;
      padding-top: 37px;
      padding-bottom: 17px;

      .xxx {
        margin-top: -3px;
      }
    }

    .i_lef {
      width: 128px;
      font-size: 16px;
      color: #1a1a1a;
    }

    .i_rig {
      display: flex;
      width: 640px;
      justify-content: space-between;

      .i_com {
        font-size: 16px;
        font-weight: 600;
        color: #1a1a1a;
      }

      .xxx {
        width: 35px;
      }

      .i_con {
        .cc {
          display: flex;
          font-size: 16px;
          margin-top: 5px;

          .c_b {
            flex: 1;
            color: #e02020;
            margin-left: 10px;
          }
        }
      }
    }

    .br {
      border-bottom: 1px solid #e5e5e5;
    }
  }

  .fen {
    display: flex;
    align-items: center;
    width: 792px;
    height: 80px;
    background: #ffece8;
    margin: 32px auto 0;

    .jf {
      flex: 1;
      text-align: center;

      p {
        font-size: 24px;
        font-weight: 600;
        color: #e02020;
      }

      h5 {
        font-size: 16px;
        font-weight: 600;
        color: #000000;
      }
    }

    .fist {
      height: 50px;
      border-right: 1px solid #e02020;
    }
  }

  .cenboxs {
    padding-top: 24px;

    .header_cen {
      display: flex;

      .card_name {
        .aa {
          font-size: 16px;
          font-weight: 600;
          color: #1a1a1a;
          margin-top: 8px;
          margin-bottom: 12px;
        }

        .bb {
          display: flex;
          width: 670px;
          justify-content: space-between;
          font-size: 16px;
          font-weight: normal;

          .myxg {
            font-size: 12px;
          }

          .el-icon {
            font-size: 16px;
          }

          .xxx {
            font-size: 16px;
            margin-top: -3px;
          }
        }
      }

      .header_img {
        img {
          width: 80px;
          height: 80px;
          border-radius: 50%;
          margin: 0 16px;
        }
      }
    }
  }
}

.mytx {
  margin-left: 25px;
  line-height: 24px;
}

.avatar-uploader-icon {
  background-color: pink !important;
  margin-left: 25px;
  font-size: 16px !important;
  font-weight: 500;
  color: #e02020 !important;
}
</style>
